package com.renfei.multimediatask;

import android.Manifest;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.renfei.multimediatask.adapter.OpenGLCourseAdapter;
import com.renfei.multimediatask.base.BaseActivity;
import com.renfei.multimediatask.ui.task1.DrawImageActivity;
import com.renfei.multimediatask.ui.task10.VideoPlayActivity;
import com.renfei.multimediatask.ui.task11.OpenGLAdvancedContent;
import com.renfei.multimediatask.ui.task2.AudioGatherAndPlayActivity;
import com.renfei.multimediatask.ui.task3.CamerPreviewActivity;
import com.renfei.multimediatask.ui.task4.Hard_Encode_DecodeActivity;
import com.renfei.multimediatask.ui.task5.OpenGLDrawTriangleActivity;
import com.renfei.multimediatask.ui.task6.OpenGLDrawImageActivity;
import com.renfei.multimediatask.ui.task7.AacEncodecAndDecodecActivity;
import com.renfei.multimediatask.ui.task8.VideoEncodecDecodecActivity;
import com.renfei.multimediatask.ui.task9.VideoRecordAllProcess;
import com.renfei.multimediatask.ui.task9.prefect.VideoRecordAllProcessPrefect;
import com.renfei.multimediatask.ui.task9.test.TestCameraResultData;
import com.renfei.multimediatask.ui.task9.test.TestCmaera2RenderToMediaCodecSurface;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity extends BaseActivity implements EasyPermissions.PermissionCallbacks {


    @BindView(R.id.rv_opengl_Course)
    RecyclerView rvOpenglCourse;
    private ArrayList<Course> courseList;
    private OpenGLCourseAdapter openGLCourseAdapter;


    static final String[] PERMISSIONS = new String[]{
            Manifest.permission.CAMERA
    };
    private static final int PERMISSIONS_RECORD_CAMERA = 1001;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initListener() {

        courseList = new ArrayList<>();
        courseList.add(new Course("1.在 Android 平台绘制一张图片", "使用至少 3 种不同的 API，ImageView，SurfaceView，自定义 View"));
        courseList.add(new Course("2.在 Android 平台录制并播放PCM数据",
                "使用 AudioRecord 和 AudioTrack API 完成音频 PCM 数据的采集和播放"));
        courseList.add(new Course("3.在 Android 平台使用 Camera API 进行视频的采集",
                "使用 SurfaceView、TextureView 来预览 Camera 数据，取到 NV21 的数据回调"));
        courseList.add(new Course("4.学习 Android 平台的 MediaExtractor 和 MediaMuxer API",
                "使用 MediaExtractor 和 MediaMuxer API，解析和封装 mp4 文件"));
        courseList.add(new Course("5.学习 Android 平台 OpenGL ES API，了解 OpenGL 开发的基本流程",
                "使用 OpenGL 绘制一个三角形"));
        courseList.add(new Course("6.学习 Android 平台 OpenGL ES API，学习纹理绘制",
                "使用 OpenGL 显示一张图片"));
        courseList.add(new Course("7.学习 MediaCodec API，完成音频 AAC 硬编、硬解",
                "音频AAC  硬编码，解码"));
        courseList.add(new Course("8.学习 MediaCodec API，完成视频 H.264 的硬编、硬解",
                "视频 H.264 的硬编、硬解"));
        courseList.add(new Course("9.串联整个音视频录制流程，",
                "完成音视频的采集、编码、封包成 mp4 输出"));
        courseList.add(new Course("10.串联整个音视频播放流程，",
                "完成 mp4 的解析、音视频的解码、播放和渲染"));
        courseList.add(new Course("11.进一步学习 OpenGL，视频剪裁、旋转、水印、滤镜，",
                "学习OpenGL高级特性，VBO VAO FBO等"));


        rvOpenglCourse.setLayoutManager(new LinearLayoutManager(this));

        openGLCourseAdapter = new OpenGLCourseAdapter(courseList);

        openGLCourseAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                switch (position) {
                    case 0:
//                        checkPreviewPermission();
                        startActivity(DrawImageActivity.class);
                        break;
                    case 1:
                        startActivity(AudioGatherAndPlayActivity.class);
                        break;
                    case 2:
                        startActivity(CamerPreviewActivity.class);
                        break;
                    case 3:
                        startActivity(Hard_Encode_DecodeActivity.class);
                        break;
                    case 4:
                        startActivity(OpenGLDrawTriangleActivity.class);
                        break;
                    case 5:
                        startActivity(OpenGLDrawImageActivity.class);
//                        startActivity(OpenGLDrawImageFboActivity.class);
                        break;
                    case 6:
                        startActivity(AacEncodecAndDecodecActivity.class);
                        break;
                    case 7:
                        startActivity(VideoEncodecDecodecActivity.class);
                        break;
                    case 8:
//                        startActivity(VideoRecordAllProcess.class);
                        startActivity(VideoRecordAllProcessPrefect.class);
//                        startActivity(TestCameraResultData.class);
                        break;
                    case 9:
                        startActivity(VideoPlayActivity.class);
                        break;
                    case 10:
                        startActivity(OpenGLAdvancedContent.class);
                        break;
                }
            }
        });

        rvOpenglCourse.setAdapter(openGLCourseAdapter);
    }


    private void checkPreviewPermission() {
        if (EasyPermissions.hasPermissions(this, PERMISSIONS)) {
            startPreviewPage();
        } else {
            applyPermission();
        }
    }

    private void applyPermission() {
        EasyPermissions.requestPermissions(this, "申请摄像头权限", PERMISSIONS_RECORD_CAMERA, PERMISSIONS);
    }

    private void startPreviewPage() {
//        startActivity(OpenGLPreviewCameraActivity.class);
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        if (requestCode == PERMISSIONS_RECORD_CAMERA) {
            startPreviewPage();
        }
    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        if (requestCode == PERMISSIONS_RECORD_CAMERA) {
            showToast("需要获取摄像头权限,否则无法使用");
        }
    }


}
